package utils;

import java.io.*;

public class TxtUtils {


    public static void main(String[] args) {
        combine("C:\\Users\\Administrator\\Desktop\\深圳证交所\\data\\","C:\\Users\\Administrator\\Desktop\\深圳证交所\\allData.txt");
    }

    public static void append(File file, String content) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(content+"\r\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void combine(String baseDir,String target){
        File dir = new File(baseDir);
        File[] files = dir.listFiles();
        for(File file : files){
            if(".DS_Store".equalsIgnoreCase(file.getName()))continue;
            System.out.println("开始合并："+file.getName());
            int readByte = 0;
            InputStream ins = null;
            OutputStream outs = null;
            try {
                // 打开源文件
                ins = new FileInputStream(file);
                // 打开目标文件的输出流
                outs = new FileOutputStream(target,true);
                byte[] buf = new byte[1024];
                // 一次读取1024个字节，当readByte为-1时表示文件已经读取完毕
                while ((readByte = ins.read(buf)) != -1) {
                    // 将读取的字节流写入到输出流
                    outs.write(buf, 0, readByte);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭输入输出流，首先关闭输出流，然后再关闭输入流
                if (outs != null) {
                    try {
                        outs.close();
                    } catch (IOException oute) {
                        oute.printStackTrace();
                    }
                }
                if (ins != null) {
                    try {
                        ins.close();
                    } catch (IOException ine) {
                        ine.printStackTrace();
                    }
                }
            }
        }
    }
}
